package com.snow.useviewdemo.util.rlv.decoration;

import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by zhyt
 * Date: 2022/4/8
 * Describe:
 */
public class GridItemDecoration extends RecyclerView.ItemDecoration {
    private int mSpanCount;//横条目数量
    private int mRowSpacing;//行间距
    private int mFirstTopSpacing = 0;//第一行的距Top间距
    private int mColumnSpacing;// 列间距

    /**
     * @param spanCount     列数
     * @param bottomSpacing 行间距
     * @param columnSpacing 列间距
     */
    public GridItemDecoration(int spanCount, int bottomSpacing, int columnSpacing) {
        this(spanCount, bottomSpacing, columnSpacing, 0);
    }

    public GridItemDecoration(int mSpanCount, int mRowSpacing, int mFirstTopSpacing, int mColumnSpacing) {
        this.mSpanCount = mSpanCount;
        this.mRowSpacing = mRowSpacing;
        this.mFirstTopSpacing = mFirstTopSpacing;
        this.mColumnSpacing = mColumnSpacing;
    }


    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view); // 获取view 在adapter中的位置。
        int columnPosition = position % mSpanCount;
        if (columnPosition != 0) {
            outRect.left = mColumnSpacing;
        }
        // 如果position < 行数，说明是第一行，设置距top间距，其他行则不指定行top高
        if (position < mSpanCount) {
            outRect.top = mFirstTopSpacing;
        }
        outRect.bottom = mRowSpacing;
    }
}
